<?php
namespace app\index\controller;

use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Recharge as RechargeModel;

/**
 * 寺院订单明细控制器
 * Class Index
 * @package app\index\controller
 */
class Companygongdengorder extends Base
{
    /**
     * [index  明细列表]
     * @param type 1供灯 2行善 3祈福香 4日行一善
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            if (!empty($param['username'])) {
                $map['m.username'] = ['like', "%" . $param['username'] . "%"];
            }
            if (!empty($param['tel'])) {
                $map['m.tel'] = ['like', "%" . $param['tel'] . "%"];
            }
            if (!empty($param['sex'])) {
                $map['m.sex'] = $param['sex'];
            }
            if(!empty($param['start']) || !empty($param['end'])){
                $map['c.create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            if(!empty($param['mid'])){
                $map['c.mid'] = $param['mid'];
            }
            $map['c.comid'] = $user['comid'];
//            $map['c.status'] = 1;
            if(empty($param['type'])){
                $field = 'c.id,c.mid,c.gongdeng,c.day,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                $name = '总订单';
            }else {
                $map['c.type'] = $param['type'];

                if ($param['type'] == 1) {
                    $field = 'c.id,c.mid,c.gongdeng,c.day,c.name,c.to_name,c.content,c.order_num,c.total_price,c.paytime,c.status,c.bless_img,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '供灯';
                }
                if ($param['type'] == 2) {
                    $field = 'c.id,c.mid,c.gongdeng,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '行善';
                }
                if ($param['type'] == 3) {
                    $field = 'c.id,c.mid,c.gongdeng,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '祈福香';
                }
                if ($param['type'] == 4) {
                    $field = 'c.id,c.mid,c.gongdeng,c.name,c.tel,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '日行一善';
                }
                if ($param['type'] == 5) {
                    $field = 'c.id,c.mid,c.gongdeng,c.bless_img,c.day,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '祈福蜡烛';
                }
                if ($param['type'] == 6) {
                    $field = 'c.id,c.mid,c.gongdeng,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '供十方';
                }
                if ($param['type'] == 7) {
                    $field = 'c.id,c.mid,c.gongdeng,c.day,c.name,c.to_name,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '许愿';
                }
            }
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('index.domain_name').$v['img'];
                }
                if(!empty($v['paytime'])){
                    $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看'.$name.'明细');
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [detial  财务明细]
     * @return json
     * @author [C。]
     */
    public function detial()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $rmodel = new RechargeModel;
        $data = [];
        $map = [];
        $map1 = [];
        $map2 = [];
        $map3 = [];
        $map4 = [];
        $map5 = [];
        try{
            $gongdeng = $model->getSum(['status'=>1,'comid'=>$param['comid']],'total_price');
            $act = $rmodel->getSum(['comid'=>$param['comid'],'states'=>1],'amt');
            $data['all_price'] = round($act + $gongdeng,2);

            $today = strtotime(date('Y-m-d',time()));
            $map['create_time'] = ['between',[$today,$today+24*60*60]];
            $map['comid'] = $param['comid'];
            $map['status'] = 1;
            $data['today_price'] = $model->getSum($map,'total_price');

            $map1['create_time'] = ['between',[$today-24*60*60,$today]];
            $map1['comid'] = $param['comid'];
            $map1['status'] = 1;
            $data['yestoday_price'] = $model->getSum($map1,'total_price');

            $map2['comid'] = $param['comid'];
            $map2['status'] = 1;
            $map2['type'] = 1;
            $map2['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_gongdeng_price'] = $model->getSum($map2,'total_price');

            $data['today_gongdeng_num'] = $model->getAllCount($map2);

            $data['all_gongdeng_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>1],'total_price');

            $map3['comid'] = $param['comid'];
            $map3['status'] = 1;
            $map3['type'] = 3;
            $map3['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_qifu_price'] = $model->getSum($map3,'total_price');

            $data['today_qifu_num'] = $model->getAllCount($map3);

            $data['all_qifu_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>3],'total_price');

            $map4['comid'] = $param['comid'];
            $map4['status'] = 1;
            $map4['type'] = 4;
            $map4['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_daydeed_price'] = $model->getSum($map4,'total_price');

            $data['today_daydeed_num'] = $model->getAllCount($map4);

            $data['all_daydeed_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>4],'total_price');

            $map5['comid'] = $param['comid'];
            $map5['status'] = 1;
            $map5['type'] = 2;
            $map5['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_deed_price'] = $model->getSum($map5,'total_price');

            $data['today_deed_num'] = $model->getAllCount($map5);

            $data['all_deed_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>2],'total_price');

            $map6['comid'] = $param['comid'];
            $map6['status'] = 1;
            $map6['type'] = 5;
            $map6['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_lazhu_price'] = $model->getSum($map6,'total_price');
            $data['today_lazhu_num'] = $model->getAllCount($map6);
            $data['all_lazhu_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>5],'total_price');

            $map7['comid'] = $param['comid'];
            $map7['status'] = 1;
            $map7['type'] = 7;
            $map7['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_xuyuan_price'] = $model->getSum($map7,'total_price');
            $data['today_xuyuan_num'] = $model->getAllCount($map7);
            $data['all_xuyuan_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>7],'total_price');

            $map8['comid'] = $param['comid'];
            $map8['status'] = 1;
            $map8['type'] = 6;
            $map8['create_time'] = ['between',[$today,$today+24*60*60]];
            $data['today_huanyuan_price'] = $model->getSum($map8,'total_price');
            $data['today_huanyuan_num'] = $model->getAllCount($map8);
            $data['all_huanyuan_price'] = $model->getSum(['comid'=>$param['comid'],'status'=>1,'type'=>6],'total_price');
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}